//
//  MYRegisterSetUserNameVC.m
//  MuYing
//
//  Created by tbb on 2019/7/22.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYRegisterSetUserNameVC.h"
#import "MYRegisterSetPerfectInfoVC.h"

typedef enum :NSUInteger{
    YQPUserNameLogin,
    YQPUserNameInfoCompelte,
} YQPUserInfo;

@interface MYRegisterSetUserNameVC ()

@property (assign, nonatomic) YQPUserInfo type;

@end

@implementation MYRegisterSetUserNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.type = YQPUserNameLogin;
}
-(void)setupView {
    [super setupView];
    [self.listTableView registerNib:[UINib nibWithNibName:@"MYAccountsCommonHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"MYAccountsCommonHeaderView"];
    [self.listTableView registerClass:[MYRegisterFindPasswordTextFieldCell class] forCellReuseIdentifier:@"MYRegisterFindPasswordTextFieldCell"];
    
    [self.listTableView registerClass:[MYLoginBaseBtnCell class] forCellReuseIdentifier:@"MYLoginBaseBtnCellDoneKey"];
    [self.listTableView registerClass:[MYLoginBaseBtnCell class] forCellReuseIdentifier:@"MYLoginBaseBtnCell"];
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MYRegisterFindPasswordTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYRegisterFindPasswordTextFieldCell"];
        cell.placeholderStr = kYQLS(@"请输入用户名");
        return cell;
    } else if(indexPath.row == 1){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kTableViewCell"];
        if (!cell){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"kTableViewCell"];
        }
        cell.contentView.backgroundColor = UIColor.whiteColor;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if(indexPath.row == 2) {
        MYLoginBaseBtnCell *loginCell = [tableView dequeueReusableCellWithIdentifier:@"MYLoginBaseBtnCellDoneKey"];
        [loginCell.loginBtn setBackgroundColor:HexColor(0x79C4C7)];
        [loginCell.loginBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
        [loginCell.loginBtn addTarget:self action:@selector(doneAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [loginCell.loginBtn setTitle:kYQLS(@"完成并登录") forState:(UIControlStateNormal)];
        return loginCell;
    }else{
        MYLoginBaseBtnCell *loginTypeCell = [tableView dequeueReusableCellWithIdentifier:@"MYLoginBaseBtnCell"];
        [loginTypeCell.loginBtn setBackgroundColor:UIColor.whiteColor];
        [loginTypeCell.loginBtn setTitleColor:HexColor(0x79C4C7) forState:(UIControlStateNormal)];
        [loginTypeCell.loginBtn addTarget:self action:@selector(perfectAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [loginTypeCell.loginBtn setTitle:kYQLS(@"继续完善个人信息") forState:(UIControlStateNormal)];
    
        return loginTypeCell;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 56;
    }if (indexPath.row == 1) {
        return 32;
    } else {
        return 68;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MYAccountsCommonHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYAccountsCommonHeaderView"];
    headerView.titleLb.text = kYQLS(@"设置用户名");
    headerView.subheadLb.text = kYQLS(@"用户名可用于登录");
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 115;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
-(void)doneAction:(UIButton *)sender {
    
    MYRegisterFindPasswordTextFieldCell *cell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

    if (cell.textField.text.length == 0){
        [MBProgressHUD showSuccess:kYQLS(@"请输入用户名")];
        return;
    }
    [self updateUserNameApi:cell.textField.text];
}
-(void)perfectAction:(UIButton *)sender {
    MYRegisterFindPasswordTextFieldCell *cell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    self.type = YQPUserNameInfoCompelte;

    if (cell.textField.text.length == 0){
        [MBProgressHUD showSuccess:kYQLS(@"请输入用户名")];
        return;
    }
    
    [self updateUserNameApi:cell.textField.text];
    
}

#pragma mark - 设置用户名
- (void)updateUserNameApi:(NSString *)username
{
    NSMutableDictionary *dic = NSMutableDictionary.new;
    dic[@"userId"] = self.userId;
    dic[@"username"] = username;

    [YQPNetWorkingManager sendPostRequest:kPOST_COMPLETEINFORMATION_URL parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [MBProgressHUD showSuccess:responseObject.msg];
        if (responseObject.code == 0) {
            if (self.type == YQPUserNameLogin) {
                [self.navigationController popToRootViewControllerAnimated:true];
            }else{
                MYRegisterSetPerfectInfoVC *perfectInfoVC = [[MYRegisterSetPerfectInfoVC alloc] init];
                perfectInfoVC.userId = self.userId;
                perfectInfoVC.username = username;

                kYQPUSH(perfectInfoVC);
            }
        }

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [MBProgressHUD showSuccess:error];
    }];
}
@end
